package com.zxy.ider.ehome.db;

import android.util.Log;

import org.litepal.crud.DataSupport;

import java.io.UnsupportedEncodingException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;

/**
 * Created by Eric on 2017/11/3.
 */

public class UpInfo extends DataSupport {
    private String sendtime;
    private String MAC;
    private String checkcode;
    private String Playertype;
    private String Usetype;
    private String Uservalue;
    private String Starttime;
    private String Endtime;
    private String UseIp;
    private String AllInfo;
    public UpInfo (String MAC,String Playertype,String Usetype,String Uservalue,String Starttime,String Endtime ){
        this.MAC = MAC;
        this.Playertype = Playertype;
        this.Usetype = Usetype;
        this.Uservalue = Uservalue;
        this.Starttime = Starttime;
        this.Endtime = Endtime;
        this.AllInfo = MAC+Playertype+Usetype+Uservalue+Starttime+Endtime;
    }
    public UpInfo(String MAC,String Playertype,String Uservalue,String Starttime){
        this.MAC = MAC;
        this.Playertype = Playertype;
        this.Uservalue = Uservalue;
        this.Starttime = Starttime;
        this.AllInfo =MAC+Playertype+Uservalue+Starttime;
    }
    public String getAllInfo(){
        return this.AllInfo;
    }
    public String getUpInfo(){
        this.sendtime = System.currentTimeMillis()+"";
        this.UseIp = getHostIP();
        try {
            this.checkcode = getMD5(sendtime+MAC+"114e464dfd41eeaea7c8033e3a4aece4");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "<?xml version='1.0' encoding='utf-8' standalone='yes' ?><item><checktext><sendtime>"+
                this.sendtime+"</sendtime><MAC>"+this.MAC+"</MAC><checkcode>"+this.checkcode+
                "</checkcode></checktext><record><post><Playertype>"+this.Playertype+"</Playertype><MAC>"+
                this.MAC+"</MAC><Usetype>"+this.Usetype+"</Usetype><Uservalue>"+this.Uservalue+
                "</Uservalue><Starttime>"+this.Starttime+"</Starttime><Endtime>"+this.Endtime+
                "</Endtime><UseIp>"+this.UseIp+"</UseIp></post></record></item>";
    }
    public String getSendtime(){
        return this.sendtime;
    }
    public void setSendtime(String sendtime){
        this.sendtime = sendtime;
    }
    public String getMAC(){
        return this.MAC;
    }
    public void setMAC(String MAC){
        this.MAC = MAC;
    }
    public String getCheckcode(){
        return this.checkcode;
    }
    public void setCheckcode(String checkcode){
        this.checkcode = checkcode;
    }

    public String getPlayertype(){
        return this.Playertype;
    }
    public void setPlayertype(String Playertype){
        this.Playertype = Playertype;
    }
    public String getUsetype(){
        return this.Usetype;
    }
    public void setUsetype(String Usetype){
        this.Usetype = Usetype;
    }
    public String getUservalue(){
        return this.Uservalue;
    }
    public void setUservalue(String Uservalue){
        this.Uservalue = Uservalue;
    }
    public String getStarttime(){
        return this.Starttime;
    }
    public void setStarttime(String Starttime){
        this.Starttime = Starttime;
    }
    public String getEndtime(){
        return this.Endtime;
    }
    public void setEndtime(String Endtime){
        this.Endtime = Endtime;
    }
    public String getUseIp(){
        return this.UseIp;
    }
    public void setUseIp(String UseIp){
        this.UseIp = UseIp;
    }

    @Override
    public boolean equals(Object object){
        if (object instanceof UpInfo){
            UpInfo upInfo = (UpInfo) object;
            if (upInfo.getAllInfo().equals(this.AllInfo)){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    @Override
    public int hashCode(){
        return 2;
    }
    public  String getMD5(String val) throws NoSuchAlgorithmException{
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(val.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }
    public String getHostIP() {

        String hostIp = null;
        try {
            Enumeration nis = NetworkInterface.getNetworkInterfaces();
            InetAddress ia = null;
            while (nis.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) nis.nextElement();
                Enumeration<InetAddress> ias = ni.getInetAddresses();
                while (ias.hasMoreElements()) {
                    ia = ias.nextElement();
                    if (ia instanceof Inet6Address) {
                        continue;// skip ipv6
                    }
                    String ip = ia.getHostAddress();
                    if (!"127.0.0.1".equals(ip)) {
                        hostIp = ia.getHostAddress();
                        break;
                    }
                }
            }
        } catch (SocketException e) {
            Log.i("yao", "SocketException");
            e.printStackTrace();
        }
        return hostIp;

    }

}
